<?php
use yii\helpers\Url;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<script type="text/javascript" src="/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="/js/ajax141212.js"></script>
<script type="text/javascript" src="/js/MultiTask.js"></script>


<div id="infopanel" style="margin:0px auto; width:1000px;">
  <div>任务总数：</div>
  <div>任务进度：0</div>
</div>


<br>
<div style="margin:0px auto; width:1000px;">
  <div id="showinfo" style="width:998px; height:200px; border:1px solid #CCC; overflow-y:auto"></div>
  <div style="height:30px; line-height:30px;text-align:center">
    <input id="button_start" type="button" value="开始"/> 
    <input id="button_pause" type="button" value="暂停"/> 
    <input id="button_stop" type="button" value="停止"/>
  </div>
</div>
<script language="javascript">

var tasks = <?php echo json_encode($tasks)?>;

var mt = new MultiTask(tasks, 200);

mt.onTask = function(key, task){
	
	console.log(task);
	
	//var data = "id="+id;

	new Ajax().get({
		url : "<?php echo Url::to(['/task/async-fav'])?>"+"?id="+task,
		timeout : 20000,
		resultType : "json",
		onLoaded : function(result){
			console.log(result);
			if(result.code == 1){
				var msg = "任务["+(key+1)+"]： 单词："+task+"完成！";
				console.log(msg);
				addinfo(msg, '#009900');
				$($('#infopanel').children('div').get(1)).html("任务进度："+(key+1)+"/"+tasks.length);
				mt.next();
			}else if(result.code == -2 || result.code == -3){
				var msg = "任务["+(key+1)+"]： 单词："+task+"词库没有或抓取失败！跳过此数据";
				console.log(msg);
				addinfo(msg, '#000099');
				$($('#infopanel').children('div').get(1)).html("任务进度："+(key+1)+"/"+tasks.length);
				mt.next();
			}else{
				addinfo(result.info+"失败");
				addinfo("任务 "+key+" 超时，5秒后自动重试");
				mt.retry();
			}
		},
		onTimeout : function(){
			addinfo("任务["+(key+1)+"] 《"+task.title+"》 超时，5秒后自动重试", '#FF0000');
			setTimeout(function(){
				mt.retry();
			}, 5000);
		}
	});
	
}

function addinfo(info, color){
	if(!arguments[1])color = '#000000';

	var text = $('#showinfo').html();
	info = '<div style="color:'+color+'">'+info+'</div>';
	text = text + info;
	
	$('#showinfo').html(text);
	$('#showinfo')[0].scrollTop = $('#showinfo')[0].scrollHeight;//滚动条在最下方
}
	
document.getElementById("button_start").onclick = function(){
	addinfo("任务开始：", '#009900');
	mt.start();
}

document.getElementById("button_pause").onclick = function(){
	mt.pause();
}

document.getElementById("button_stop").onclick = function(){
	mt.stop();
}
	
mt.onEnd = function(){
	addinfo("任务完成！", '#009900');
};

(function(){
	$($('#infopanel').children('div').get(0)).html("任务总数："+tasks.length);	
})();
	
</script>

</body>
</html>
